/*
 * main.cpp
 *
 *  Created on: 28-04-2013
 *      Author: Skeith
 */
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

#include "Utilities.h"

int main(int argc, char** argv) {
	//cv::Mat m = cv::imread("female_20_normal.jpg");
	cv::Mat m = cv::imread("male_25_normal.jpg");
	//cv::Mat m = cv::imread("female_48_normal.jpg");
	//cv::Mat m = cv::imread("male_45_normal.jpg");

	std::vector<cv::Mat> splits;
	cv::split(m, splits);

	cv::cvtColor(m, m, CV_BGR2GRAY);

	cv::Mat faceImg;
	cv::Mat copyFaceImg;
	extractFace(m, faceImg);
	faceImg.copyTo(copyFaceImg);

	//Finding face size
	int right, left;
	int size = faceSize(faceImg, left, right);
	std::cout << size << std::endl;

	cv::line(copyFaceImg, cv::Point(left, 0), cv::Point(left, faceImg.rows), cv::Scalar(255, 255, 255), 1, 1, 0);
	cv::line(copyFaceImg, cv::Point(right, 0), cv::Point(right, faceImg.rows), cv::Scalar(255, 255, 255), 1, 1, 0);

	//Eyes section
	int eyeLine = findEyesLine(faceImg);
	cv::line(copyFaceImg, cv::Point(0, eyeLine), cv::Point(faceImg.cols, eyeLine), cv::Scalar(255, 255, 255), 1, 1, 0);

	cv::Mat rEye, lEye;
	cv::Rect rEyeRect, lEyeRect;

	findEyes(faceImg, eyeLine, rEye, lEye, rEyeRect, lEyeRect);

	cv::Point rEyeCenter, lEyeCenter;
	int rEyeRadius, lEyeRadius;

	findEyeCenter(rEye, rEyeCenter, rEyeRadius);
	findEyeCenter(lEye, lEyeCenter, lEyeRadius);

	rEyeCenter.x += rEyeRect.x;
	rEyeCenter.y += rEyeRect.y;

	lEyeCenter.x += lEyeRect.x;
	lEyeCenter.y += lEyeRect.y;

	cv::circle(copyFaceImg, rEyeCenter, 3, cv::Scalar(255, 0, 0), 1, 2, 0);
	//cv::circle(copyFaceImg, rEyeCenter, rEyeRadius, cv::Scalar(255, 0, 0), 1, 2, 0);

	cv::circle(copyFaceImg, lEyeCenter, 3, cv::Scalar(255, 0, 0), 1, 2, 0);
	//cv::circle(copyFaceImg, lEyeCenter, lEyeRadius, cv::Scalar(255, 0, 0), 1, 2, 0);

	cv::rectangle(copyFaceImg, rEyeRect, cv::Scalar(255, 255, 255), 1, 1);
	cv::rectangle(copyFaceImg, lEyeRect, cv::Scalar(255, 255, 255), 1, 1);

	//-------------------------------------------------------//

	//nose
	cv::Mat nose;
	cv::Rect noseRect = findNose(copyFaceImg);

	copyFaceImg(noseRect).copyTo(nose);

	cv::adaptiveThreshold(nose, nose, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 7, 10);

	cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(1 * 2 + 1, 1 * 2 + 1), cv::Point(1, 1));
	cv::blur(nose, nose, cv::Size(2, 2));
	//cv::morphologyEx(nose, nose, cv::MORPH_CLOSE, element);

	cv::dilate(nose, nose, element);

	//cv::morphologyEx(nose, nose, cv::MORPH_CLOSE, element);

	cv::imshow("nose", nose);
	cv::rectangle(copyFaceImg, noseRect, cv::Scalar(255, 255, 255), 1, 1, 0);

	//----------------------------//

	//mouth

	cv::Mat mouth;
	cv::Rect mouthRect = findMouth(copyFaceImg);

	copyFaceImg(mouthRect).copyTo(mouth);

	cv::adaptiveThreshold(mouth, mouth, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 9, 15);

	//cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(1 * 2 + 1, 1 * 2 + 1), cv::Point(1, 1));
	//cv::blur(nose, nose, cv::Size(2, 2));
	//cv::morphologyEx(nose, nose, cv::MORPH_CLOSE, element);

	//cv::dilate(nose, nose, element);

	//cv::morphologyEx(nose, nose, cv::MORPH_CLOSE, element);

	cv::imshow("mouth", mouth);
	cv::rectangle(copyFaceImg, mouthRect, cv::Scalar(255, 255, 255), 1, 1, 0);

	//---------------------------------//

	//cv::GaussianBlur(faceImg, faceImg, cv::Size(3, 3), 0, 0, cv::BORDER_DEFAULT);

	//cv::Mat histogram;
	//createHistogram(faceImg, histogram);

	cv::Mat hProjection;
	cv::Mat vProjection;
	createProjections(nose, hProjection, vProjection);

	cv::Mat hProjectionImg;
	cv::Mat vProjectionImg;
	createProjectionHistogram(hProjection, hProjectionImg, 32, 0);
	createProjectionHistogram(vProjection, vProjectionImg, 32, 1);

	cv::imshow("Horizontal Projection", hProjectionImg);
	cv::imshow("Vertical Projection", vProjectionImg);
	//cv::imshow("Histogram", histogram);
	cv::imshow("Face Image", faceImg);
	cv::imshow("Copy Face Image", copyFaceImg);
	cv::imshow("Img", m);

	cv::waitKey(0);
	return 0;
}

